package com.ruoyi.web.controller.mall;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.Dict;
import com.ruoyi.system.domain.Order;
import com.ruoyi.system.domain.OrderRefund;
import com.ruoyi.system.domain.RefundRecord;
import com.ruoyi.system.domain.vo.*;
import com.ruoyi.system.mapper.DictMapper;
import com.ruoyi.system.mapper.OrderMapper;
import com.ruoyi.system.mapper.OrderRefundMapper;
import com.ruoyi.system.mapper.RefundRecordMapper;
import com.ruoyi.web.controller.tool.AlipayTradeWapPay;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/refund")
public class RefundController extends BaseController {

    @Resource
    private OrderRefundMapper orderRefundMapper;
    @Resource
    private OrderMapper orderMapper;
    @Resource
    private RefundRecordMapper refundRecordMapper;
    @Resource
    private DictMapper dictMapper;

    @GetMapping("/queryList")
    public TableDataInfo queryList(RefundQueryVo vo) {
        startPage();
        List<RefundDetailVo> list = orderRefundMapper.queryList(vo);
        return getDataTable(list);
    }

    @GetMapping("/approved/{id}")
    public AjaxResult approved(@PathVariable Long id) {
        orderRefundMapper.updateStatus(id,2);
        return success();
    }

    @GetMapping("/reject/{id}")
    public AjaxResult reject(@PathVariable Long id) {
        orderRefundMapper.updateStatus(id,5);
        return success();
    }

    @GetMapping("/queryDict")
    public AjaxResult queryDict() {
        List<Dict> dictList = dictMapper.selectByType(2);
        return success(dictList);
    }

    @PostMapping("/submit")
    public AjaxResult addTrackNo(@RequestBody RefundReqVo vo) throws AlipayApiException {
        OrderRefund orderRefund = orderRefundMapper.selectById(vo.getId());
        AlipayModel model = new AlipayModel();
        model.setOrderSn(orderRefund.getOrderSn());
        model.setMoney(vo.getPrice().multiply(new BigDecimal(vo.getCount())));
        String refund = AlipayTradeWapPay.refund(model);
        JSONObject jsonObject = JSONObject.parseObject(JSONObject.parseObject(refund).getString("alipay_trade_refund_response"));
        String msg = jsonObject.getString("msg");
        if ("Success".equals(msg)){
            orderRefund.setUpdateTime(new Date());
            orderRefund.setStatus(3);
            orderRefundMapper.updateById(orderRefund);
            orderMapper.updateStatus(orderRefund.getOrderSn(),5);
            RefundRecord record = new RefundRecord();
            record.setRefundId(orderRefund.getId());
            record.setMsg(msg);
            record.setContent(refund);
            record.setCreateTime(new Date());
            record.setCount(vo.getCount());
            record.setPrice(vo.getPrice());
            record.setSysUser(SecurityUtils.getUserId());
            refundRecordMapper.insert(record);
        } else {
            return error("退款失败");
        }
        return success();
    }

}
